﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Register.Model;
using Register.BLL;
using Register.DAL;
using System.Web.Script.Serialization;
using Register.Command;
using System.Text;
namespace RegisterWeb.Manager.DoctorWorkDayManager.ashx
{
    /// <summary>
    /// DoctorWorkDayManagerServer 的摘要说明
    /// </summary>
    public class DoctorWorkDayManagerService : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");

            String action = context.Request["action"].ToString();
            if (action.Equals("list"))
            {
                int row = int.Parse(context.Request["rows"].ToString());
                int page = int.Parse(context.Request["page"].ToString());
                List<DoctorWorkDay> list = new List<DoctorWorkDay>();
                list = DoctorWorkDayBLL.GetPagedDoctorWorkDay(row, page, " DoctorWorkDay_State=1 ");

                List<DoctorWorkDayJSON> json = new List<DoctorWorkDayJSON>();
                foreach (DoctorWorkDay DoctorWorkDay in list)
                {
                    json.Add(new DoctorWorkDayJSON(DoctorWorkDay.DoctorWorkDay_ID,DoctorWorkDay.Doctor_ID, getDoctor(DoctorWorkDay.Doctor_ID), DoctorWorkDay.DoctorWorkDay_TimeType, getTimeType(DoctorWorkDay.DoctorWorkDay_TimeType), DoctorWorkDay.DoctorWorkDay_DayTime.ToString(), DoctorWorkDay.DoctorWorkDay_AddTime.ToString(), DoctorWorkDay.DoctorWorkDay_Recoder, DoctorWorkDay.DoctorWorkDay_State));
                }

                DoctorWorkDayListJSON DoctorWorkDayListJSON = new DoctorWorkDayListJSON();
                DoctorWorkDayListJSON.total = DoctorWorkDayBLL.GetDoctorWorkDayCount(null);
                DoctorWorkDayListJSON.rows = json;

                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(DoctorWorkDayListJSON));
            }
            else if (action.Equals("delete"))
            {
                String DoctorWorkDay_ID = context.Request["id"];
                if (DoctorWorkDayBLL.DeleteDoctorWorkDay(DoctorWorkDay_ID))
                    context.Response.Write("ok");
                else
                    context.Response.Write("no");
            }
            else if (action.Equals("add"))
            {
                DoctorWorkDay DoctorWorkDay = new DoctorWorkDay();
                DoctorWorkDay.DoctorWorkDay_ID = DataIDHelper.GetDataID("DoctorWorkDay_ID");
                DoctorWorkDay.Doctor_ID = context.Request["Doctor_Name"];
                DoctorWorkDay.DoctorWorkDay_TimeType = context.Request["Type"];
                DoctorWorkDay.DoctorWorkDay_Recoder = "admin";///////////////////////////////////////////////////////////////////////////记录
                DoctorWorkDay.DoctorWorkDay_AddTime = DateTime.Now;
                DoctorWorkDay.DoctorWorkDay_DayTime = DateTime.Parse(context.Request["DayTime"]);
                DoctorWorkDay.DoctorWorkDay_State = "1";
                DoctorWorkDay.DoctorWorkDay_Departments_ID = getDepartments_ID(DoctorWorkDay.Doctor_ID);
                
                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(new message(DoctorWorkDayBLL.AddDoctorWorkDay(DoctorWorkDay), "添加成功！")));

            }
            else if (action.Equals("edit"))
            {
                DoctorWorkDay DoctorWorkDay = new DoctorWorkDay();
                DoctorWorkDay.DoctorWorkDay_ID = context.Request["ID"];
                DoctorWorkDay.Doctor_ID = context.Request["Doctor_Name"];
                DoctorWorkDay.DoctorWorkDay_TimeType = context.Request["Type"];
                DoctorWorkDay.DoctorWorkDay_DayTime = DateTime.Parse(context.Request["DayTime"]);
                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(new message(DoctorWorkDayBLL.UpdateDoctorWorkDay(DoctorWorkDay), "编辑成功！")));

            }

            else if (action.Equals("search"))
            {
                string TimeType = context.Request["TimeType"];
                string Doctor_ID = context.Request["Doctor_ID"];
                string Begin = context.Request["Begin"];
                string End = context.Request["End"];
                DoctorWorkDayListJSON doctorWorkDayListJSON = new DoctorWorkDayListJSON();
                doctorWorkDayListJSON.rows = new List<DoctorWorkDayJSON>();

                int row = int.Parse(context.Request["rows"].ToString());
                int page = int.Parse(context.Request["page"].ToString());

                StringBuilder strBuilder = new StringBuilder();
                strBuilder.Append(" DoctorWorkDay_State='1' ");
                if (!String.IsNullOrEmpty(Doctor_ID))
                {
                    strBuilder.Append(" and  Doctor_ID='").Append(Doctor_ID).Append("' ");
                }
                if (!String.IsNullOrEmpty(TimeType))
                {
                    strBuilder.Append(" and  DoctorWorkDay_TimeType='").Append(TimeType).Append("' ");
                }
                if (!(String.IsNullOrEmpty(Begin) || String.IsNullOrEmpty(End)))
                {
                    strBuilder.Append(" and DoctorWorkDay_DayTime  Between '").Append(Begin).Append("' and '").Append(End).Append("'");
                }
                List<Register.Model.DoctorWorkDay> list = DoctorWorkDayBLL.GetPagedDoctorWorkDay(row, page, strBuilder.ToString());
                doctorWorkDayListJSON.total = DoctorWorkDayBLL.GetDoctorWorkDayCount(strBuilder.ToString());


                foreach (Register.Model.DoctorWorkDay doctorWorkDay in list)
                {
                    doctorWorkDayListJSON.rows.Add(new DoctorWorkDayJSON(doctorWorkDay.DoctorWorkDay_ID, doctorWorkDay.Doctor_ID, getDoctor(doctorWorkDay.Doctor_ID), doctorWorkDay.DoctorWorkDay_TimeType, getTimeType(doctorWorkDay.DoctorWorkDay_TimeType), doctorWorkDay.DoctorWorkDay_DayTime.ToString(), doctorWorkDay.DoctorWorkDay_AddTime.ToString(), doctorWorkDay.DoctorWorkDay_Recoder, doctorWorkDay.DoctorWorkDay_State));
                }


                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(doctorWorkDayListJSON));
            }
            else if (action.Equals("check"))
            {
                string name = context.Request["ID"];
                if (AdminInfoBLL.Exists(name))
                {
                    context.Response.Write("ok");
                }
                else
                {
                    context.Response.Write("no");
                }
            }
            else if (action.Equals("Type"))
            {
                List<Parameter> list;
                JavaScriptSerializer jss = new JavaScriptSerializer();

                list = ParamterHelper.GetParametersByType("Scheduling_Time");
                context.Response.Write(jss.Serialize(list));
            }
            else if (action.Equals("Name"))
            {
                List<DoctorInfo> list;
                JavaScriptSerializer jss = new JavaScriptSerializer();

                list = DoctorWorkDayBLL.GetDoctorInfo();
                context.Response.Write(jss.Serialize(list));
            }

        }

        //获得时间类型
        public String getTimeType(String TimeType)
        {
            String TextTimeType = "";
            List<Parameter> list;
            JavaScriptSerializer jss = new JavaScriptSerializer();

            list = ParamterHelper.GetParametersByType("Scheduling_Time");
            foreach (Parameter parameter in list)
            {
                if (parameter.SystemParameter_ID == TimeType)
                    TextTimeType = parameter.SystemParameter_Name;
            }
            return TextTimeType;
        }
        //获得医生姓名
        public String getDoctor(String Doctor_ID)
        {
            String DoctorName = "";
            DoctorInfo list;
            JavaScriptSerializer jss = new JavaScriptSerializer();

            list = DoctorInfoBLL.GetDoctorInfo(Doctor_ID);
            DoctorName = list.Doctor_Name;
            return DoctorName;
        }

        //获得科室
        public String getDepartments_ID(String Doctor_ID)
        {
            String DoctorName = "";
            DoctorInfo list;
            JavaScriptSerializer jss = new JavaScriptSerializer();

            list = DoctorInfoBLL.GetDoctorInfo(Doctor_ID);
            DoctorName = list.Departments_ID;
            return DoctorName;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

    class message
    {
        public message(bool success, string msg)
        {
            this.msg = msg;
            this.success = success;
        }
        public bool success { get; set; }
        public string msg { get; set; }
    }

    class DoctorWorkDayListJSON
    {

        public int total { get; set; }
        public List<DoctorWorkDayJSON> rows { get; set; }

    }

    class DoctorWorkDayJSON
    {
        public String ID { get; set; }
        public String Doctor_ID { get; set; }
        public String Doctor_Name { get; set; }
        public String TimeType { get; set; }
        public String TextTimeType { get; set; }
        public String DayTime { get; set; }
        public String AddTime { get; set; }
        public String Recoder { get; set; }
        public String State { get; set; }

        public DoctorWorkDayJSON(String ID, String Doctor_ID, String Doctor_Name, String TimeType, String TextTimeType, String DayTime, String AddTime, String Recoder, String State)
        {
            this.ID = ID;
            this.Doctor_ID = Doctor_ID;
            this.Doctor_Name = Doctor_Name;
            this.TimeType = TimeType;
            this.TextTimeType = TextTimeType;
            this.DayTime = DayTime;
            this.AddTime = AddTime;
            this.Recoder = Recoder;
            this.State = State;
        }

    }
}